﻿// Decompiled with JetBrains decompiler
// Type: MCM.Common.SelectedIndexWrapper
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using HarmonyLib.BUTR.Extensions;
using System;
using System.Collections.Concurrent;

#nullable enable
namespace MCM.Common
{
  public readonly ref struct SelectedIndexWrapper
  {
    private static readonly ConcurrentDictionary<Type, SelectedIndexWrapper.GetSelectedIndexDelegate?> _getSelectedIndexCache = new ConcurrentDictionary<Type, SelectedIndexWrapper.GetSelectedIndexDelegate>();
    private static readonly ConcurrentDictionary<Type, SelectedIndexWrapper.SetSelectedIndexDelegate?> _setSelectedIndexCache = new ConcurrentDictionary<Type, SelectedIndexWrapper.SetSelectedIndexDelegate>();
    private readonly SelectedIndexWrapper.GetSelectedIndexDelegate? _getSelectedIndex;
    private readonly SelectedIndexWrapper.SetSelectedIndexDelegate? _setSelectedIndex;
    private readonly object? _object;

    public int SelectedIndex
    {
      get
      {
        SelectedIndexWrapper.GetSelectedIndexDelegate getSelectedIndex = this._getSelectedIndex;
        return getSelectedIndex == null ? -1 : getSelectedIndex(this._object);
      }
      set
      {
        SelectedIndexWrapper.SetSelectedIndexDelegate setSelectedIndex = this._setSelectedIndex;
        if (setSelectedIndex == null)
          return;
        setSelectedIndex(this._object, value);
      }
    }

    public SelectedIndexWrapper(object? @object)
    {
      this._object = @object;
      Type type = @object?.GetType();
      this._getSelectedIndex = (object) type != null ? SelectedIndexWrapper._getSelectedIndexCache.GetOrAdd(type, (Func<Type, SelectedIndexWrapper.GetSelectedIndexDelegate>) (t => AccessTools2.GetPropertyGetterDelegate<SelectedIndexWrapper.GetSelectedIndexDelegate>(t, nameof (SelectedIndex), false))) : (SelectedIndexWrapper.GetSelectedIndexDelegate) null;
      this._setSelectedIndex = (object) type != null ? SelectedIndexWrapper._setSelectedIndexCache.GetOrAdd(type, (Func<Type, SelectedIndexWrapper.SetSelectedIndexDelegate>) (t => AccessTools2.GetPropertySetterDelegate<SelectedIndexWrapper.SetSelectedIndexDelegate>(t, nameof (SelectedIndex), false))) : (SelectedIndexWrapper.SetSelectedIndexDelegate) null;
    }

    private delegate int GetSelectedIndexDelegate(object instance);

    private delegate void SetSelectedIndexDelegate(object instance, int value);
  }
}
